package dbops

import (
	"time"
	_ "database/sql"
)

func init() {
	go WeeHoursTask(func() {
		//ClearRedundantData()
	})
}

//定时任务
func WeeHoursTask(task func()) {
	for {
		now := time.Now()
		next := now.Add(time.Hour * 24)
		next = time.Date(next.Year(), next.Month(), next.Day(), 0, 0, 0, 0, next.Location())
		//loge.W("下次执行时间" + next.Format("2006-01-02 15:04:05"))
		//next = time.Date(next.Year(), next.Month(), next.Day(), next.Hour(), next.Minute(), 0, 0, next.Location())
		t := time.NewTimer(next.Sub(now))
		<-t.C
		go task()
	}
}
func ClearRedundantData() error {
	//清除无效的模板数据
	stmtOut, err := dbConn.Prepare("DELETE FROM table_tree_template WHERE tree_node_id NOT IN (SELECT node_id FROM table_tree);")
	if err != nil {
		return err
	}
	defer stmtOut.Close()
	_, err = stmtOut.Exec()
	return err
}
